package conf

import (
	"encoding/json"
	"log"

	"github.com/spf13/viper"
)

const (
	Release = "release"
)

type Config struct {
	App         App
	Log         Log
	Server      Server
	Database    Database
	Redis       Redis
	Sms         Sms
	AliOSS      AliOss
	VerifyToken VerifyToken
}

type App struct {
	AppName string
}

type Log struct {
	LogSavePath string
	LogLevel    string
	LogMaxSize  int
	LogMaxAge   int
}

type Server struct {
	RunMode       string
	HttpPort      int
	RpcPort       int
	WebSocketPort int
	ReadTimeout   int
	WriteTimeout  int
}

type Database struct {
	Type        string
	User        string
	Password    string
	Host        string
	Name        string
	TablePrefix string
}

type Redis struct {
	Host string
	DB   int
}

type Sms struct {
	SmsSendUrl string
	Src        string
	Token      string
}

type AliOss struct {
	Endpoint        string
	AccessKeyId     string
	AccessKeySecret string
	BucketName      string
	UrlPrefix       string
	LocalPath       string
}

type VerifyToken struct {
	VerifyUrl string
}

var CONFIG Config

func InitConfig() {
	v := viper.New()
	v.SetConfigFile("conf/push.yaml")
	err := v.ReadInConfig()
	if err != nil {
		log.Fatal("Fatal error ReadInConfig: \n", err)
	}

	if err = v.Unmarshal(&CONFIG); err != nil {
		log.Fatal("Fatal error Unmarshal: \n", err)
	}

	log.Println("===============初始化conf/push.yaml配置成功===============")
	marshalIndent, _ := json.MarshalIndent(&CONFIG, "", "\t")
	log.Println(string(marshalIndent))
	log.Println()
	log.Println()
}
